<?php

declare(strict_types=1);

namespace App\Model\Act;

use App\Model\CallBase;
use App\Model\Moment;
use App\Model\PayOrder;
use App\Model\UserProfile;
use App\Model\VoiceCall;
use Hyperf\DbConnection\Db;
use Hyperf\DbConnection\Model\Model;

/**
 * 活动排行榜
 */
class UserRankAct extends Model
{
    protected $table = 'user_rank_activity';

    //女神榜单
    const ACT_HOST_RANK = 1;

    protected $fillable = [
        'user_id', 'dt', 'act_id', 'rank', 'flag', 'rank_value'
    ];

    public function userItem()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function userProfile()
    {
        return $this->belongsTo(UserProfile::class, 'user_id', 'user_id');
    }

    /**
     * 添加记录
     */
    static function addRecord($userId = 0, $rankValue = 0, $rank = 1, $actId = 1, $date = null)
    {
        $model = new self();
        $model->user_id = $userId;
        $model->act_id = $actId;
        $model->rank_value = $rankValue;
        $model->rank = $rank;
        $model->dt = $date ?: date('Y-m-d');
        return $model->saveOrFail() ? 1 : 0;
    }

    /**
     * 获取数据
     */
    static function getList($week = 1, $actId = self::ACT_HOST_RANK)
    {
        switch ($week) {
            case 1:
                $date = date('Y-m-d', strtotime('monday'));
                break;
            case 2:
                $date = date('Y-m-d', strtotime('last monday'));
                break;
        }
        $where = [
            'act_id' => $actId,
            // 'dt' => $date,
            'flag' => 1
        ];
        return self::where($where)->orderBy('rank', 'asc')->limit(20)->get()->toArray();
    }

    /**
     * 获取用户排名信息
     */
    static function getUserRank($userId = 0)
    {
        $item = self::where('user_id', $userId)->first();
        if ($item) {
            return [
                'rank' => $item->rank,
                'rank_value' => $item->rank_value,
                'left_value' => 0,
            ];
        }
        $leastRank = self::orderBy('rank', 'desc')->first();
        return [
            'rank' => '100+',
            'rank_value' => 0,
            'left_value' => $leastRank['rank_value'],
        ];
    }

    /**
     * 获取邀请人充值的
     */
    static function getInvitedRechargers($start = '', $end = '')
    {
        $data = Db::table('pay_order')->join('user_profile', function ($join) use ($start, $end) {
            $where = [
                ['pay_order.status', PayOrder::STATUS_OK],
                ['role', UserProfile::ROLE_HOST],
                ['invite_code', '<>', ''],
            ];
            if ($start) $where[] = ['finished_at', '>=', $start];
            if ($end) $where[] = ['finished_at', '<', $end];

            $join->on('pay_order.user_id', '=', 'user_profile.user_id')->where($where);
        })->select(['pay_order.user_id', 'invite_code'])->get()->toArray();
        $info = [];
        foreach ($data as $item) {
            if (!isset($info[$item->invite_code])) $info[$item->invite_code] = [];
            if (!in_array($item->user_id, $info[$item->invite_code])) array_push($info[$item->invite_code], $item->user_id);
        }
        return $info;
    }

    /**
     * 声优开启聊天的记录
     */
    static function getHostChatRecords($start = '', $end = '')
    {
        return Db::table('user_chat')->join('user_profile', function ($join) use ($start, $end) {
            $where = [
                ['user_profile.role', UserProfile::ROLE_HOST],
                ['user_chat.flag', 1],
                ['user_chat.msg_num', '>', 0],
            ];
            if ($start) $where[] = ['user_chat.created_at', '>=', $start];
            if ($end) $where[] = ['user_chat.created_at', '<', $end];
            $join->on('user_chat.user_id', '=', 'user_profile.user_id')->where($where);
        })->select(['user_chat.user_id', 'chat_user_id', 'msg_num', 'chat_msg_num'])->get()->toArray();
    }

    /**
     * 获取数据
     */
    static function getHostVoicecalls($start = '', $end = '')
    {
        $where = [
            ['call_time', '>', 0]
        ];
        if ($start) $where[] = ['created_at', '>=', $start];
        if ($end) $where[] = ['created_at', '<', $end];
        return VoiceCall::where($where)->whereIn('status', CallBase::DONE_STATUS)->select(['chat_user_id', 'call_time'])->get()->toArray();
    }

    /**
     * 发布动态记录
     */
    static function getMoments($start = '', $end = '')
    {
        $where = [
            ['status', Moment::STATUS_OK],
        ];
        if ($start) $where[] = ['created_at', '>=', $start];
        if ($end) $where[] = ['created_at', '<', $end];
        return Moment::where($where)->get()->toArray();
    }
}
